using System;
using System.IO;

using Vibstudio.X4NET.IO.INI.Model;
using Vibstudio.X4NET.IO.INI.Model.Formatting;

namespace Vibstudio.X4NET.IO.INI
{
    public class StreamIniDataParser
    {
        public IniDataParser Parser { get; protected set; }

        protected StreamIniDataParser() : this (new IniDataParser()) {}

        public StreamIniDataParser(IniDataParser parser)
        {
            Parser = parser;
        }

        protected IniData ReadData(StreamReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");
            
            return Parser.Parse(reader.ReadToEnd());
        }

        protected void WriteData(StreamWriter writer, IniData iniData)
        {
            if (iniData == null)
                throw new ArgumentNullException("iniData");
            if (writer == null)
                throw new ArgumentNullException("writer");

            writer.Write(iniData.ToString());
        }

        public void WriteData(StreamWriter writer, IniData iniData, IIniDataFormatter formatter)
        {
            if (formatter == null)
                throw new ArgumentNullException("formatter");
            if (iniData == null)
                throw new ArgumentNullException("iniData");
            if (writer == null)
                throw new ArgumentNullException("writer");

            writer.Write(iniData.ToString(formatter));
        }
    }
}
